package main

import (
	"fmt"
	"strconv"

	"gitee.com/fcsvr/fc/fclog"
	"github.com/tidwall/gjson"
)

func CheckAlpha(index int, alphaid string) {
	//fclog.Info("CheckAlpha alphaid = %s", alphaid)
	url := "https://api.worldquantbrain.com/alphas/" + alphaid
	resp, _ := G_Client.R().SetHeader("Content-Type", "application/json").Get(url)
	strBody := string(resp.Body())
	fitness := gjson.Get(strBody, "is.fitness").Float()
	sharpe := gjson.Get(strBody, "is.sharpe").Float()
	longcount := gjson.Get(strBody, "is.longCount").Int()
	shortcount := gjson.Get(strBody, "is.shortCount").Int()
	fclog.Info("CheckAlpha index = %d, AlphaID = %s, fitness = %v, sharpe = %v, long = %d, short = %d", index, alphaid, fitness, sharpe, longcount, shortcount)
	if fitness == 0 && sharpe == 0 && longcount == 0 && shortcount == 0 {
		fclog.Info("CheckAlpha index %d, %s, %v", index, strBody, resp.Header())
		return
	}
	/*
		if ((fitness < 0.8) && (fitness > -0.8)) || ((sharpe < 1.4) && (sharpe > -1.4)) || ((longcount <= 10) && (shortcount <= 10)) {
			SetAlphaHidden(alphaid)
			return
		}
	*/
	//ppac new rule
	if ((sharpe < 1) && (sharpe > -1)) || ((longcount <= 50) && (shortcount <= 50)) {
		SetAlphaHidden(alphaid)
		return
	}

	failNum := 0
	checksLen := gjson.Get(strBody, "is.checks.#").Int()
	//fclog.Debug("Checkalpha checks len = %v", checksLen)
	for i := 0; i < int(checksLen)-2; i++ {
		index := "is.checks." + strconv.Itoa(i) + ".result"
		result := gjson.Get(strBody, index).String()
		if result == "FAIL" || result == "ERROR" {
			failNum = 1
			break
		}
		//fclog.Debug("Checkalpha result = %s", result)
	}
	if failNum > 0 {
		fclog.Error("Checkalpha %s Fail", alphaid)
		return
	}

	color := "BLUE"
	alphaCorr, sharpeSlice := CheckSelfCorr(alphaid)
	fclog.Debug("checkselfcoor = %v, %v", alphaCorr, sharpeSlice)
	if alphaCorr > 0.505 {
		for _, v := range sharpeSlice {
			if sharpe/v < 1.095 {
				color = "YELLOW"
				break
			}
		}
	}
	if alphaCorr == -1 {
		SetAlphaHidden(alphaid)
		return
	}
	SetAlphaColor(alphaid, color)
}

func CheckSubmitAlphas(startdate string, enddate string, fitness float32, sharpe float32, region string) {
	alphas := make(map[string]bool)
	offset := 0
	baseurl := "https://api.worldquantbrain.com/users/self/alphas?limit=100&offset=%d&status=UNSUBMITTED%%1FIS_FAIL&dateCreated%%3E%sT00:00:00-04:00&dateCreated%%3C%sT00:00:00-04:00&is.fitness%%3E%f&is.sharpe%%3E%f&settings.region=%s&order=-is.sharpe&hidden=false&type!=SUPER"
	url := fmt.Sprintf(baseurl, offset, startdate, enddate, fitness, sharpe, region)
	//fmt.Println(url)

	resp, _ := G_Client.R().SetHeader("Content-Type", "application/json").Get(url)
	strBody := string(resp.Body())
	count := gjson.Get(strBody, "count").Int()
	fclog.Info("Alpha good count = %d", count)
	for i := 0; i < int(count); i += 100 {
		url = fmt.Sprintf(baseurl, i, startdate, enddate, fitness, sharpe, region)
		resp, _ := G_Client.R().SetHeader("Content-Type", "application/json").Get(url)
		strBody := string(resp.Body())
		for j := 0; j < 100; j++ {
			index := "results." + strconv.Itoa(j)
			if !gjson.Get(strBody, index).Exists() {
				break
			}
			index_id := index + ".id"
			alpha := gjson.Get(strBody, index_id).String()
			alphas[alpha] = true
		}
	}

	for k, _ := range alphas {
		CheckAlpha(1, k)
	}
}
func CheckSelfCorr(alphaid string) (float64, []float64) {
	alphaPnl := GetAlphaPnl(alphaid)
	x := make([]float64, G_PnlDays, G_PnlDays)
	sharpeSlice := make([]float64, 0, 0)
	j := 0
	recordsLen1 := len(alphaPnl.Vals)
	fclog.Debug("CheckSelfCorr = %s, recordsLen = %d", alphaid, recordsLen1)
	//check diff 0
	diff0Count := 0
	for i := recordsLen1 - G_DiffDays; i < recordsLen1; i++ {
		val0 := alphaPnl.Vals[i-1].Val
		val1 := alphaPnl.Vals[i].Val
		if val0 == val1 {
			diff0Count++
		}
		if diff0Count > 50 {
			return -1, sharpeSlice
		}
	}
	for i := recordsLen1 - G_PnlDays; i < recordsLen1; i++ {
		val0 := alphaPnl.Vals[i-1].Val
		val1 := alphaPnl.Vals[i].Val
		x[j] = val1 - val0
		j++
	}
	//fclog.Debug("%v", recordsLen1)

	alphaCorr := 0.0
	for _, v := range G_ActiveAlphaPnl.AlphaPnls {
		y := make([]float64, G_PnlDays, G_PnlDays)
		recordsLen := len(v.Vals)
		j := 0
		for i := recordsLen - G_PnlDays; i < recordsLen; i++ {
			val0 := v.Vals[i-1].Val
			val1 := v.Vals[i].Val
			y[j] = val1 - val0
			j++
		}
		//corr := stat.Correlation(x, y, nil)
		//fclog.Debug("CheckSelfCorr alphaid = %s, x len = %d, y len = %d", alphaid, len(x), len(y))
		corr, err := PearsonCorrelation(x, y)
		if err != nil {
			fclog.Warn("CheckSelfCorr err = %v", err)
		}
		//fclog.Debug("CheckSelfCorr alpha= %v, sharpe = %v, corr = %v", v.Alphaid, v.Sharpe, corr)
		if corr > 0.505 {
			sharpeSlice = append(sharpeSlice, v.Sharpe)
		}
		if corr > alphaCorr {
			alphaCorr = corr
		}
	}
	fclog.Info("CheckSelfCorr corr = %v", alphaCorr)
	return alphaCorr, sharpeSlice
}
